/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package charlie.shop.warehouse;


import charlie.shop.misc.Common;
import com.charlie.beans.Warehouse;
import com.charlie.sessions.WarehouseFacadeLocal;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Vitalii D.
 */

@WebServlet(name = "EditWH", urlPatterns = {"/warehouse/EditWH"})
public class EditWH extends HttpServlet {

    @EJB
    private WarehouseFacadeLocal warehouse;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Warehouse currentWH;
        int whId = Common.getParametrInt(request, "whId");
        currentWH = warehouse.find(whId);
        request.setAttribute("wh", currentWH);
        getServletContext().getRequestDispatcher("/warehouse/editWH/editWH.jsp").forward(request, response);
        
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        if (request.getParameter("Save") == null) {
            processRequest(request, response);
        }
        else{
            
            Warehouse currentWH;
            int whId = Common.getParametrInt(request, "whId");
            currentWH = warehouse.find(whId);

            int ship_rate =  Common.getParametrInt(request,"shipmentRate");
            int ship_dur = Common.getParametrInt(request,"shipmentDuration");
            currentWH.setShipmentDuration(ship_rate);
            currentWH.setShipmentDuration(ship_dur);

            warehouse.edit(currentWH);

            getServletContext().getRequestDispatcher("/admin/completeEdition.jsp").forward(request, response);
            
        }
        
        
        
    }    
}
